package com.jadesoft.stock;

import java.util.ArrayList;
import java.util.List;

import com.jadesoft.common.service.PaginationService;
import com.jadesoft.stock.entity.WeatherSubscript;

public class WeatherSubBSImpl implements WeatherSubBS {
	private PaginationService persistence = new PaginationService();

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<WeatherSubscript> list(WeatherSubscript subscript) {
		String hql = "from WeatherSubscript sub " + " where sub.userId = ? ";
		List params = new ArrayList();
		params.add(subscript.getUserId());
		if (subscript.getId() != null) {
			hql += " and sub.id = ? ";
			params.add(subscript.getId());
		}
		hql += " Order by  sub.updateTime desc";
		List<WeatherSubscript> result = (List<WeatherSubscript>) persistence.query(
				hql, params.toArray());
		return result;
	}

	public void remove(WeatherSubscript subscript) {
		if (this.list(subscript).size() > 0)
			persistence.removeObj(WeatherSubscript.class, subscript.getId());
	}

	public WeatherSubscript save(WeatherSubscript subscript) {
		if (subscript.getId() == null) {
			return (WeatherSubscript) persistence.saveObj(subscript);
		} else {
			this.persistence.updateObj(subscript);
			return subscript;
		}
	}

}
